# -*- coding: utf-8 -*-
"""
Created on Wed May  9 11:45:05 2018

@author: river
"""
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('Qt5Agg')

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as pltCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as pltToolbar

import sys, os
import xlrd
from PyQt5 import QtCore, QtGui, QtWidgets

import main_ui, filter_param

class MainWindow(QtWidgets.QMainWindow, main_ui.Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.initUI()

        self.fireA = []     #A col is fire
        self.humanB = []    #B col is human sensor
        self.rejectC = []   #C col is interface

    def initUI(self):
        self.figure = plt.Figure()
        self.ax = self.figure.add_subplot(111)
        self.canvas = pltCanvas(self.figure)
        self.toolbar = pltToolbar(self.canvas, self)

        self.MatPlotVLayout.addWidget(self.toolbar)
        self.MatPlotVLayout.addWidget(self.canvas)
        self.setLayout(self.MatPlotVLayout)

        self.OpenFileButton.clicked.connect(self.open_file)
        self.DrawButton.clicked.connect(self.draw)
        self.FilterButton.clicked.connect(self.filterparam)

        self.filterPoint = 5
        self.filename = ""
        self.refresh = False
        self.drawFlag = False

    def _draw(self):
        self.ax.clear()
        self.ax.plot(range(len(self.fireA)), self.fireA, 'r', range(len(self.humanB)), self.humanB, 'g', range(len(self.rejectC)), self.rejectC, 'b')
        self.canvas.draw()

    def open_file(self):
        self.filename, _ = QtWidgets.QFileDialog.getOpenFileName(self, "选择文件", "", "Excel File(*.xls);;All File(*)")
        if self.filename:
            self.FileTxtEdit.setText(self.filename)
            self.refresh = True
            self.drawFlag = False
            self.fireA = []
            self.humanB = []
            self.rejectC = []
        else:
            self.refresh = False
            self.drawFlag = False

    def draw(self):
        if self.refresh:
            try:
                wb = xlrd.open_workbook(self.filename)
                for s in wb.sheets():
                    for row in range(s.nrows):
                        val = []
                        for col in range(s.ncols):
                            val.append(s.cell(row, col).value)
                        if s.ncols == 3:
                            self.fireA.append(val[0])
                            self.humanB.append(val[1])
                            self.rejectC.append(val[2])
                        else:
                            self.refresh = False
                            break
                self.refresh = True
                self._draw()
            except Exception as e:
                QtWidgets.QMessageBox.warning(self, "warning", str(e)[:64])
                pass

    def filterparam(self):
        pass

class ParamForm(QtWidgets.QWidget, filter_param.Ui_FormFilterParam):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

    def show_param(self):
        self.show()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)  #create a QApp
    root = MainWindow()    #create a window for all ctrls
    filter_param = ParamForm()

    root.ParamButton.clicked.connect(filter_param.show_param)
    #ui = main_ui.Ui_MainWindow()  #instance main_ui
    #ui.setupUi(MainWindow)
    root.show()
    sys.exit(app.exec_())